package org.lanqiao.dao;

import java.sql.Timestamp;
import java.util.List;
import java.util.UUID;
import org.lanqiao.pojo.UserPOJO;
import org.lanqiao.util.SqlUtil;

public class UserDaoImpl {
    /**
     * 查询用户的方法,使用用户名和密码。
     */
    public List select (String userName,String password) {
        String sql = "select * from user where user_name = ? and user_password = ? and user_status = 1";
        return SqlUtil.select3(sql, userName,password);
    }
    /**
     * 通过手机号，查询用户是否存在
     */
    public List selectTel(String telphone) {
        String sql="select user_id,user_name,user_telphone from user where user_telphone = ? and user_status = 1";
        return SqlUtil.select3(sql, telphone);
    }
    /**
     * 通过用户名，查询用户是否存在
     */
    public List selectUserName(String userName) {
        String sql="select user_id,user_name,user_telphone from user where user_name = ? and user_status = 1";
        return SqlUtil.select3(sql, userName);
    }
    /**
     * 通过用户Id查询用户
     */
    public List selectUserId(String userId) {
        String sql = "select * from user where user_id = ? and user_status = 1";
        return SqlUtil.select3(sql, userId);
    }
    /**
     * 注册用户的方法
     */
    public List insert(UserPOJO user) {
        String sql="insert into user (user_id,user_name,user_password,user_sex,user_age,user_birthday,user_photo,"
                + "user_telphone,user_address,user_comment,user_date_time) values (?,?,?,?,?,?,?,?,?,?,?)";
        
        //动态获得UUID
        String uuid = UUID.randomUUID().toString();
        java.util.Date date = new java.util.Date();          // 获取一个Date对象
        Timestamp timeStamp = new Timestamp(date.getTime());     //   讲日期时间转换为数据库中的timestamp类型
        String name = user.getUserName();
        String password = user.getPassword();
        String sex = user.getSex();
        String age = user.getAge();
        String birthday = user.getBirthday();
        String photo = user.getPhoto();
        String telphone = user.getTelphone();
        String address = user.getAddres();
        String comment = user.getComment();
        
        SqlUtil.update(sql, uuid,name,password,sex,age,birthday,photo,telphone,address,comment,timeStamp);
        return new UserDaoImpl().select(name, password);
    }
    /**
     * 修改用户的方法
     */
    public List update(UserPOJO user) {
        String sql="update user set user_name = ?,user_password = ?,user_sex = ?,user_age = ?,user_birthday = ?,"
                + "user_photo = ?,user_telphone = ?,user_address = ?,user_status = ?,user_comment = ? where user_id = ?";
                
        String userId=user.getUserId();
        String name=user.getUserName();
        String password=user.getPassword();
        String sex=user.getSex();
        String age=user.getAge();
        String birthday=user.getBirthday();
        String photo=user.getPhoto();
        String telphone=user.getTelphone();
        String address=user.getAddres();
        String comment=user.getComment();
        Integer status=Integer.valueOf(user.getStatus());
        
        int row=0;
        List list=null;
        row=SqlUtil.update(sql, name,password,sex,age,birthday,photo,telphone,address,status,comment,userId);
        if(row>0) {
            list=new UserDaoImpl().select(name, password);
        }
        return list; 
    }
}
